/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.third.daikai.daoda;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.third.daikai.daoda.constant.DaodaConstant;
import org.jeecg.modules.third.daikai.daoda.dto.AddOrderDTO;
import org.jeecg.modules.third.daikai.daoda.dto.AddUserDTO;
import org.jeecg.modules.third.jg.report.freight.information.util.WebUtils;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.modules.third.daikai.daoda.dto.AddUserDriverDTO;
import org.jeecg.modules.third.daikai.daoda.dto.AddUserVehicleDTO;
import org.jeecg.modules.third.daikai.daoda.dto.OrderLineDTO;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 司机代开-道达
 *
 * @author duanlikao
 * @created 2021/7/1
 */
@Service
public class DaodaService {
    private static final Logger LOGGER = LoggerFactory.getLogger(DaodaService.class);
//    @Value(value="${driver-daoda.domain}")
//    private static String DAODA_DOMAIN = "https://www.dda56.com/" ;


    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;

    //银行文件路径
    @Value(value = "${jeecg.path.bank}")
    private String bankPath;

    /**
     * 图片上传
     * @param imgUrl
     * @return
     */
    public  String uploadFiles(String imgUrl) {

        if(StringUtils.isEmpty(imgUrl)){
            LOGGER.info("DaodaService uploadFiles isEmpty:{}", imgUrl);
            return StringUtils.EMPTY;
        }
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getDkDaodaIzUse()){
            throw new JeecgBootException("司机代开-道达服务暂未开启,请联系管理员。");
        }
        String pathTmp =  uploadpath  + File.separator +imgUrl;
        //文件名是pdf改成jpg
        String suffix = imgUrl.substring(imgUrl.lastIndexOf("."));
        if(suffix.equals(".pdf")){
            pathTmp =  uploadpath  + File.separator + imgUrl.substring(0, imgUrl.lastIndexOf(".")) + ".jpg";
            File file = new File(pathTmp);
            if (!file.exists()) {
                pathTmp =  bankPath  + File.separator +  imgUrl.substring(0, imgUrl.lastIndexOf(".")) + ".jpg";
            }
        }
        File file = new File(pathTmp);
        if (!file.exists()) {
            LOGGER.info("DaodaService uploadFiles exists:{}" ,pathTmp);
            return StringUtils.EMPTY;
        }
        try{
            String urlPath = DaodaConstant.DAODA_DOMAIN +"Api/FileUpload.aspx";
            Map <String, String> map = new HashMap <>();
            map.put("action", DaodaConstant.ACTION_TYPE_UPLOADFILES);
            map.put("token", systemConfig.getDkDaodaToken());
            map.put("AdminID",systemConfig.getDkDaodaAdminid());
//            LOGGER.info("DaodaService uploadFiles map:{}",map);

            String buildQuery = WebUtils.buildQuery(map, WebUtils.DEFAULT_CHARSET);
            URL url =  WebUtils.buildGetUrl(urlPath, buildQuery);
            String result = WebUtils.uploadImgInNews(url,file);
//            LOGGER.info("DaodaService uploadFiles result:{}",result);
            if(StringUtils.isNotEmpty(result)){
                JSONObject jsonObject = JSONObject.parseObject(result);
                return jsonObject.getString("url") != null ?  jsonObject.getString("url") : StringUtils.EMPTY ;
            }
            return StringUtils.EMPTY;
        }catch (Exception e){
            LOGGER.info("DaodaService uploadFiles Exception:{}",e.getMessage());
        }
        return StringUtils.EMPTY;
    }

    /**
     * 用户注册接口
     * @param addUserDTO
     * @return
     */
    public  String AddUser(AddUserDTO addUserDTO)  {
        if(addUserDTO == null ){
            return StringUtils.EMPTY;
        }
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getDkDaodaIzUse()){
            throw new JeecgBootException("司机代开-道达服务暂未开启,请联系管理员。");
        }
        try {
            addUserDTO.setAction(DaodaConstant.ACTION_TYPE_ADDUSER);
            addUserDTO.setToken(systemConfig.getDkDaodaToken());
            addUserDTO.setAdminID(systemConfig.getDkDaodaAdminid());
            String url = DaodaConstant.DAODA_DOMAIN + "Api/User.aspx";
//            LOGGER.info("DaodaService Adduser url:{} addUserDTO:{} ", url,JSONObject.toJSONString( addUserDTO));
            String result = WebUtils.doPostBuildQuery(url, CommonUtils.beanToMap(addUserDTO), WebUtils.DEFAULT_CHARSET, null);
//            LOGGER.info("DaodaService Adduser result:{}", result);
            if (StringUtils.isNotEmpty(result)) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                boolean success = jsonObject.getBoolean("success") != null ? jsonObject.getBoolean("success") : false;
                if (success) {
                    return result;
                }
            }
            return StringUtils.EMPTY;
        }catch (IOException ioe){
            LOGGER.info("DaodaService AddUser IOException:{}",ioe.getMessage());
        }
        return StringUtils.EMPTY;
    }

    /**
     * 司机认证接口
     * @param adduserDriverDTO
     * @return
     */
    public  String AdduserDriver(AddUserDriverDTO adduserDriverDTO){
        if(adduserDriverDTO == null ){
            return StringUtils.EMPTY;
        }
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getDkDaodaIzUse()){
            throw new JeecgBootException("司机代开-道达服务暂未开启,请联系管理员。");
        }
        try {
            adduserDriverDTO.setAction(DaodaConstant.ACTION_TYPE_ADDUSER_DRIVER);
            adduserDriverDTO.setToken(systemConfig.getDkDaodaToken());
            adduserDriverDTO.setAdminID(systemConfig.getDkDaodaAdminid());
            String url = DaodaConstant.DAODA_DOMAIN + "Api/User.aspx";
//            LOGGER.info("DaodaService AdduserDriver url:{} adduserDriverDTO:{} ", url, JSONObject.toJSONString(adduserDriverDTO));
            String result = WebUtils.doPostBuildQuery(url, CommonUtils.beanToMap(adduserDriverDTO), WebUtils.DEFAULT_CHARSET, null);
//            LOGGER.info("DaodaService AdduserDriver result:{}", result);
            return StringUtils.isNotEmpty(result) ? result : StringUtils.EMPTY;
        }catch (IOException ioe){
            LOGGER.info("DaodaService AdduserDriver IOException:{}",ioe.getMessage());
        }
        return StringUtils.EMPTY;
    }

    /**
     * 车辆认证接口
     * @param addUserVehicleDTO
     * @return
     */
    public  String addUserVehicle(AddUserVehicleDTO addUserVehicleDTO){
        if(addUserVehicleDTO == null ){
            return StringUtils.EMPTY;
        }
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getDkDaodaIzUse()){
            throw new JeecgBootException("司机代开-道达服务暂未开启,请联系管理员。");
        }
        try {
            addUserVehicleDTO.setAction(DaodaConstant.ACTION_TYPE_ADDUSER_VEHICLE);
            addUserVehicleDTO.setToken(systemConfig.getDkDaodaToken());
            addUserVehicleDTO.setAdminID(systemConfig.getDkDaodaAdminid());
            String url = DaodaConstant.DAODA_DOMAIN + "Api/User.aspx";
//            LOGGER.info("DaodaService addUserVehicle url:{} addUserVehicleDTO:{} ", url, JSONObject.toJSONString(addUserVehicleDTO));
            String result = WebUtils.doPostBuildQuery(url, CommonUtils.beanToMap(addUserVehicleDTO), WebUtils.DEFAULT_CHARSET, null);
//            LOGGER.info("DaodaService addUserVehicle result:{}", result);
            return StringUtils.isNotEmpty(result) ? result : StringUtils.EMPTY;
        }catch (IOException ioe){
            LOGGER.info("DaodaService addUserVehicle IOException:{}",ioe.getMessage());
        }
        return StringUtils.EMPTY;
    }

    /**
     * 更新车辆信息
     * @param addUserVehicleDTO
     * @return
     */
    public  String updateUserVehicle(AddUserVehicleDTO addUserVehicleDTO){
        if(addUserVehicleDTO == null ){
            return StringUtils.EMPTY;
        }
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getDkDaodaIzUse()){
            throw new JeecgBootException("司机代开-道达服务暂未开启,请联系管理员。");
        }
        try {
            addUserVehicleDTO.setAction(DaodaConstant.ACTION_TYPE_UPDATEUSER_VEHICLE);
            addUserVehicleDTO.setToken(systemConfig.getDkDaodaToken());
            addUserVehicleDTO.setAdminID(systemConfig.getDkDaodaAdminid());
            String url = DaodaConstant.DAODA_DOMAIN + "Api/User.aspx";
//            LOGGER.info("DaodaService updateUserVehicle url:{} addUserVehicleDTO:{} ", url, JSONObject.toJSONString(addUserVehicleDTO));
            String result = WebUtils.doPostBuildQuery(url, CommonUtils.beanToMap(addUserVehicleDTO), WebUtils.DEFAULT_CHARSET, null);
//            LOGGER.info("DaodaService updateUserVehicle result:{}", result);
            return StringUtils.isNotEmpty(result) ? result : StringUtils.EMPTY;
        }catch (IOException ioe){
            LOGGER.info("DaodaService updateUserVehicle IOException:{}",ioe.getMessage());
        }
        return StringUtils.EMPTY;
    }

    /**
     * 订单信息接口
     * @param addOrderDTO
     * @return
     */
    public  String addOrder(AddOrderDTO addOrderDTO){
        if(addOrderDTO == null ){
            return StringUtils.EMPTY;
        }
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getDkDaodaIzUse()){
            throw new JeecgBootException("司机代开-道达服务暂未开启,请联系管理员。");
        }
        try {

            addOrderDTO.setAction(DaodaConstant.ACTION_TYPE_ADDORDER);
            addOrderDTO.setToken(systemConfig.getDkDaodaToken());
            addOrderDTO.setAdminID(systemConfig.getDkDaodaAdminid());
            addOrderDTO.setUserID(systemConfig.getDkDaodaCustomerid());
            String url = DaodaConstant.DAODA_DOMAIN + "Api/Order.aspx";
//            LOGGER.info("DaodaService addOrder url:{} addOrderDTO:{} ", url, JSONObject.toJSONString(addOrderDTO));
            String result = WebUtils.doPostBuildQuery(url, CommonUtils.beanToMap(addOrderDTO), WebUtils.DEFAULT_CHARSET, null);
//            LOGGER.info("DaodaService addOrder result:{}", result);
            return StringUtils.isNotEmpty(result) ? result : StringUtils.EMPTY;
        }catch (IOException ioe){
            LOGGER.info("DaodaService addOrder IOException:{}",ioe.getMessage());
        }
        return StringUtils.EMPTY;
    }

    /**
     * 运单行车轨迹接口
     * @param orderLineDTO
     * @return
     */
    public  String orderLine(OrderLineDTO orderLineDTO){
        if(orderLineDTO == null ){
            return StringUtils.EMPTY;
        }
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getDkDaodaIzUse()){
            throw new JeecgBootException("司机代开-道达服务暂未开启,请联系管理员。");
        }
        try {
            orderLineDTO.setAction(DaodaConstant.ACTION_TYPE_ORDERLINE);
            orderLineDTO.setToken(systemConfig.getDkDaodaToken());
            orderLineDTO.setAdminID(systemConfig.getDkDaodaAdminid());
            String url = DaodaConstant.DAODA_DOMAIN + "Api/Order.aspx";
//            LOGGER.info("DaodaService orderLine url:{} orderLineDTO:{} ", url, JSONObject.toJSONString(orderLineDTO));
            String result = WebUtils.doPostBuildQuery(url, CommonUtils.beanToMap(orderLineDTO), WebUtils.DEFAULT_CHARSET, null);
//            LOGGER.info("DaodaService orderLine result:{}", result);
            return StringUtils.isNotEmpty(result) ? result : StringUtils.EMPTY;
        }catch (IOException ioe){
            LOGGER.info("DaodaService orderLine IOException:{}",ioe.getMessage());
        }
        return StringUtils.EMPTY;
    }



    public static void main(String[] args) throws Exception {


        SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMdd");
        String date = dateF.format(new Date());
        System.out.println(date);
//        DaodaService daodaService = new DaodaService();
//
//        String imgUrl = "/Users/duanlikao/Downloads/tmp_06ffd8219650f9f72069db4b9fd2df61a31bde9b271cc77b_1621248785273_1621299464404.pdf";
//        String  url = daodaService.uploadFiles( imgUrl);
//        System.out.println(url);
//
//        AddUserDTO addUserDTO = new AddUserDTO();
//        addUserDTO.setUserName("13718988648");
//        addUserDTO.setPassword("13718988648");
//        addUserDTO.setNickname("test用户");
//        String  re = daodaService.AddUser(addUserDTO);
//        System.out.println(re);

//        String suffix = imgUrl.substring(imgUrl.lastIndexOf("."));
//        if(suffix.equals(".pdf")){
//            String a = imgUrl.substring(0, imgUrl.lastIndexOf(".")) + ".jpg";
//            System.out.println(a);
//        }
//        System.out.println(suffix);
    }

}
